package com.buyhouse.zhaimao.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.buyhouse.zhaimao.BaseFragment;
import com.buyhouse.zhaimao.R;
import com.buyhouse.zhaimao.util.DebugLog;
import com.buyhouse.zhaimao.view.CircleImageView;

/**
 * 左侧菜单 menu
 * 
 * @author zhouby
 *
 */
public class MenuFragment extends BaseFragment implements OnCheckedChangeListener  {

	private RadioGroup mRg;

	private SLMenuListClickListener mCallback;
	/** 头像 **/
	private CircleImageView mCircleImageView;

	@Override
	public void onAttach(Activity activity) {
		try {
			mCallback = (SLMenuListClickListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnResolveTelsCompletedListener");
		}
		super.onAttach(activity);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.circleImageView: //个人
			
			mRg.clearCheck();
			FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();
			beginTransaction.setTransition(R.anim.slide_right_in);
			beginTransaction.replace(R.id.content,new PersonalCenterFragment());
			beginTransaction.addToBackStack(null);
			beginTransaction.commit();
			
			break;

		default:
			break;
		}
	}

	@Override
	protected int getContentView() {

		return R.layout.fragment_menu;
	}

	@Override
	protected void initFragment() {
		mRg = (RadioGroup) findViewById(R.id.menu_radiogroup);
		mRg.setOnCheckedChangeListener(this);

		mCircleImageView = (CircleImageView) findViewById(R.id.circleImageView);
		mCircleImageView.setOnClickListener(this);
	
		FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();			
		beginTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		beginTransaction.replace(R.id.content, new NearbyExpertFragment());		
		beginTransaction.addToBackStack(null);//保留状态
		beginTransaction.commit();
		
	}

	

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		
		Fragment fragment=null;
		
		switch (group.getCheckedRadioButtonId()) {
		case R.id.radiobutton_expert: //附近专家
			fragment=new NearbyExpertFragment();
			break;
		case R.id.radiobutton_community: //社区
			fragment=new CommunityFragment();
			break;
		case R.id.radiobutton_management://日程管理
			fragment=new ManagementFragment();
			break;
		case R.id.radiobutton_chat://聊天
			fragment=new ChatFragment();
			break;
		case R.id.radiobutton_message://消息列表
			fragment=new MessageFragment();
			break;

		default:
			break;
		}
		
		if(null!=fragment){
			FragmentTransaction beginTransaction = getFragmentManager().beginTransaction();			
			beginTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
			beginTransaction.replace(R.id.content, fragment);
			beginTransaction.addToBackStack(null);//保留状态
			beginTransaction.commit();
		}else{
			DebugLog.e("Error:fragment is null");
		}
		if(mCallback!=null){
			mCallback.selectListener();
		}	
	}

	/**
	 * menu 点击接口
	 * 
	 * @author zhouby
	 *
	 */
	public interface SLMenuListClickListener {
		/**
		 * menu 点击方法
		 */
		public void selectListener();

		/**
		 * @param id
		 *            view的id
		 */
		public void selectId(int id, Fragment fragmenManager);
		

	}

}
